﻿<style>
    .form_title { padding: 10px 50px; margin-bottom: 10px; }
    .form_title b { font-size: 16px; }
    .tbsum { color: #666; font-weight: bold; line-height: 30px; font-size: 20px; width: 100%; margin-left: -2px; margin-top: -2px; border-right: solid 1px #94C0D2; }
    .tbsum tr td { background: #fff; border-left: solid 1px #94C0D2; border-bottom: solid 1px #94C0D2; }
    .divTitle { line-height: 35px; margin-bottom: 5px; padding: 0px 5px; display: none; position: relative; }
    .divTitle button { position: absolute; right: 5px; top: 3px; }
    .divTitle span { margin-right: 30px; }
    .house { width: 200px; }
</style>

<div class="ld-tabs">
    <ul>
        <li class="k-state-active">盘存单</li>
        <li>调拨单</li>
    </ul>
    <div>
        <div class="k-block form_title">
            <div style="display: inline-block;">
                <b>仓库：</b><input id="txtWarehouse" class="house" />
            </div>
            <div style="display: inline-block;">
                <b>日期：</b><input id="txtNoteDate_PC" class="date" />
            </div>
        </div>
        <div id="grid_orders_pc"></div>
    </div>
    <div>
        <div class="k-block form_title">
            <div style="display: inline-block;">
                <b>源仓库：</b><input id="txtWarehouseSource" data-value="0" class="house" />
            </div>
            <div style="display: inline-block;">
                <b>目标仓库：</b><input id="txtWarehouseTarget" data-value="0" class="house" />
            </div>
            <div style="display: inline-block;">
                <b>日期：</b><input id="txtNoteDate_TB" class="date" />
            </div>
        </div>
        <div id="grid_orders_tb"></div>
    </div>
</div>

<script class="init">
    LocatedMenu("仓库管理", "盘存调拨");

    $(".date").kendoDatePicker({
        value: new Date(),
        culture: "zh-CHS",
        format: "yyyy年MM月dd日",
    });

    $(".house").kendoDropDownList({
        placeholder: "",
        optionLabel: {
            House: "请选择仓库...",
            ID: 0
        },
        dataTextField: "House",
        dataValueField: "ID",
        dataSource: Whale.dataSource({
            url: "/Orders/Warehouse/Gets"
        })
    });
</script>

<script class="grid_orders_columns">
    var orderFields = [{
        title: "商品名称",
        field: "Product",
        template: "#=Product.ProductName#",
        editor: productEditor,
        width: 260
    }, {
        title: "单位",
        field: "Product",
        template: "#=Product.UnitName#",
        width: 100
    }, {
        title: "数量",
        field: "Quantity",
        editor: quantityEditor,
        width: 100
    }, {
        title: "辅助数量",
        field: "AssistantQuantity",
        width: 100
    }, {
        title: "价值",
        field: "CostMoney",
        format: "{0:c}",
        editor: costMoneyEditor,
        width: 100
    }, {
        title: "批号",
        field: "Retention",
        editor: retentionSelectEditor,
        width: 100
    }, {
        title: "操作",
        command: [{
            name: "destroy",
            text: "删除"
        }]
    }];

    var allProducts = Whale.Remote.GetEntity({
        url: "/Products/Product/Gets",
        parameters: {
            pageSize: 0
        }
    });
    function productEditor(container, options) {
        var selPro = $('<input required data-bind="value:Product.ProductName "/>')
            .appendTo(container)
            .kendoComboBox({
                autoBind: false,
                filter: "contains",
                dataTextField: "Pinyin",
                template: "#=ProductName#",
                dataValueField: "ID",
                dataSource: allProducts,
                change: function (e) {
                    if (selPro.dataItem() == null || selPro.dataItem() == undefined) {
                        container.parents('.k-grid').data("kendoGrid").cancelRow();
                        return;
                    }
                    var product = selPro.dataItem();
                    options.model.Product = product;

                    options.model.AssistantQuantity = "";
                    options.model.Quantity = "";
                    options.model.CostMoney = "";
                    options.model.Retention = "";

                    container.parents('.k-grid').data("kendoGrid").refresh();
                }
            }).data("kendoComboBox");
    }

    function quantityEditor(container, options) {
        var txtQuantity = $('<input required data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoNumericTextBox({
                change: function (e) {
                    options.model.Quantity = txtQuantity.value();
                    var rate = options.model.Product.AssistantUnitRate;
                    if (rate != null && rate > 0) {
                        var q = options.model.Quantity;
                        var d = Math.floor(q / rate);
                        var m = q % rate;
                        if (d > 0) {
                            options.model.AssistantQuantity = d + options.model.Product.AssistantUnitName;
                            if (m > 0)
                                options.model.AssistantQuantity += m + options.model.Product.UnitName;
                        }
                    }
                    container.parents('.k-grid').data("kendoGrid").refresh();
                }
            }).data("kendoNumericTextBox");
    }

    function costMoneyEditor(container, options) {
        var txtCostMoney = $('<input data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoNumericTextBox({
                format: "{0:c}"
            }).data("kendoNumericTextBox");
    }

    function retentionSelectEditor(container, options) {
        var houseID = $("#txtWarehouse").data("kendoDropDownList").value();
        console.log("A", houseID);
        if (houseID > 0) {
            var selRetention = $('<select data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoComboBox({
                //autoBind: true,
                dataTextField: "Retention",
                dataValueField: "Retention",
                dataSource: Whale.dataSource({
                    url: "/Orders/Repertory/GetTops",
                    getParameters: function (e) {
                        return {
                            productID: options.model.Product.ID,
                            houseID: houseID,
                            repType: 3,
                            hideZero: true
                        }
                    }
                }),
                //dataBound: function (e) {
                //    var item = e.sender.dataItem();
                //    if (item != null)
                //        options.model.Retention = item.Retention;
                //},
                change: function (e) {
                    var item = selRetention.dataItem();
                    if (item != null && item != undefined) {
                        options.model.Retention = item.Retention;
                    }
                }
            }).data("kendoComboBox");
        } else {
            wndGeneralInformation.content("请先选择一个仓库。").open();
        }
    }
</script>

<script class="grid_orders_pc_config">
    var orderPcDataSource = new kendo.data.DataSource({
        schema: {
            model: {
                fields: {
                    Product: { defaultValue: { ID: 0, ProductName: "", UnitName: "" } },
                    Quantity: { validation: { required: true } },
                    Retention: {},
                    AssistantQuantity: { editable: false },
                    CostMoney: {}
                }
            }
        }
    });

    $("#grid_orders_pc").kendoGrid({
        toolbar: [{
            name: "create",
            text: "新增一行"
        }, {
            name: "save",
            text: "保存盘存单"
        }],
        columns: orderFields,
        pageable: false,
        editable: true,
        dataBound: function (e) {
            //让表头居中
            $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            $("a.k-grid-save-changes").attr("style", "font-weight:bold; float:right");
        },
        dataSource: orderPcDataSource,
        saveChanges: function (e) {
            var values = {
                orderDate: kendo.toString($("#txtNoteDate_PC").data("kendoDatePicker").value(), "yyyy-MM-dd"),
                houseID: $("#txtWarehouse").data("kendoDropDownList").value(),
                orderMethod: 13,
                orderItems: []
            };

            values.orderItems = $.map(orderPcDataSource.data(), function (o) {
                return {
                    productID: o.Product.ID,
                    quantity: o.Quantity,
                    costMoney: o.CostMoney,
                    retention: o.Retention
                }
            });
            Whale.Remote.Operation({
                url: "/Orders/ProductOrder/SetBalance",
                parameters: values,
                success: function (result) {
                    location.href = "/Success/PrintProductOrder/" + result.OrderNo;
                },
                error: function (msg) {
                    wndGeneralInformation.content(msg).open();
                }
            });
        }
    });
</script>

<script class="grid_orders_tb_config">
    var orderTbDataSource = new kendo.data.DataSource({
        schema: {
            model: {
                fields: {
                    Product: { defaultValue: { ID: 0, ProductName: "", UnitName: "" } },
                    Quantity: { validation: { required: true } },
                    Retention: {},
                    AssistantQuantity: { editable: false },
                    CostMoney: {}
                }
            }
        }
    });

    $("#grid_orders_tb").kendoGrid({
        toolbar: [{
            name: "create",
            text: "新增一行"
        }, {
            name: "save",
            text: "保存调拨单"
        }],
        columns: orderFields,
        pageable: false,
        editable: true,
        dataBound: function (e) {
            //让表头居中
            $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            $("a.k-grid-save-changes").attr("style", "font-weight:bold; float:right");
        },
        dataSource: orderTbDataSource,
        saveChanges: function (e) {
            var values = {
                orderDate: kendo.toString($("#txtNoteDate_TB").data("kendoDatePicker").value(), "yyyy-MM-dd"),
                houseID: $("#txtWarehouseSource").data("kendoDropDownList").value(),
                orderMethod: 14,
                targetHouseID: $("#txtWarehouseTarget").data("kendoDropDownList").value(),
                orderItems: []
            };

            values.orderItems = $.map(orderTbDataSource.data(), function (o) {
                return {
                    productID: o.Product.ID,
                    quantity: o.Quantity,
                    costMoney: o.CostMoney,
                    retention: o.Retention
                }
            });
            Whale.Remote.Operation({
                url: "/Orders/ProductOrder/SetBalance",
                parameters: values,
                success: function (result) {
                    location.href = "/Success/PrintProductOrder/" + result.OrderNo;
                },
                error: function (msg) {
                    wndGeneralInformation.content(msg).open();
                }
            });
        }
    });
</script>
